
package com.gitee.jmash.storage.dao;

import com.crenjoy.proto.utils.TypeUtil;
import com.gitee.jmash.core.orm.DtoPage;
import com.gitee.jmash.core.orm.DtoTotal;
import com.gitee.jmash.core.orm.jpa.BaseDao;
import com.gitee.jmash.core.orm.jpa.SqlBuilder;
import com.gitee.jmash.core.orm.jpa.TenantEntityManager;
import com.gitee.jmash.storage.entity.StorageOrganBrowserEntity;
import jmash.storage.protobuf.StorageOrganBrowserReq;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import com.gitee.jmash.storage.entity.StorageOrganBrowserEntity.StorageOrganBrowserPk;

/**
 * StorageOrganBrowser实体的Dao层（使用JPA实现）.
 *
 * @author <a href="mailto:service@crenjoy.com">crenjoy</a>
 */
public class  StorageOrganBrowserDao extends BaseDao<StorageOrganBrowserEntity, StorageOrganBrowserPk> {

  public StorageOrganBrowserDao() {
    super();
  }

  public StorageOrganBrowserDao(TenantEntityManager tem) {
    super(tem);
  }
	
  
  /**
   * 查询记录数.
   */
  public Integer findCount(StorageOrganBrowserReq req) {
    SqlBuilder sqlBuilder = createSql(req);
    String query = sqlBuilder.getQuerySql("select count(s) ");
    Object obj = this.findSingleResultByParams(query, sqlBuilder.getParams());
    return (obj == null) ? 0 : Integer.valueOf(obj.toString());
  }
  
  /**
   * 综合查询.
   */
  public List<StorageOrganBrowserEntity> findListByReq(StorageOrganBrowserReq req) {
    SqlBuilder sqlBuilder = createSql(req);
    String query = sqlBuilder.getQuerySql("select s ");
    return this.findListByParams(query, sqlBuilder.getParams());
  }

  /**
   * 综合查询Page.
   */
  public DtoPage<StorageOrganBrowserEntity, DtoTotal> findPageByReq(StorageOrganBrowserReq req) {
    SqlBuilder sqlBuilder = createSql(req);
    String query = sqlBuilder.getQuerySql("select s ");
    String totalQuery = sqlBuilder.getTotalSql("select count(s) as totalSize ");
    return this.findDtoPageByParams(req.getCurPage(), req.getPageSize(), query, totalQuery,
        DtoTotal.class, sqlBuilder.getParams());
  }

  /** Create SQL By Req . */
  public SqlBuilder createSql(StorageOrganBrowserReq req) {
    StringBuilder sql = new StringBuilder(" from StorageOrganBrowserEntity s where 1=1  ");
    Map<String, Object> params = new HashMap<String, Object>();
	
	

	

	String orderSql = "";
    if (StringUtils.isNotBlank(req.getOrderName())) {
      orderSql = String.format(" order by %s %s ", req.getOrderName(),
          req.getOrderAsc() ? " asc " : " desc ");
    }

    return SqlBuilder.build().setSql(sql).setParams(params).setOrderSql(orderSql);
  }
  

}
